package com.project.dao.imp;

import com.project.bean.CutPageBean;
import com.project.bean.OrderBean;
import com.project.dao.IOrderDao;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.time.LocalDate;

@Repository
public class OrderDaoImp extends BaseDao implements IOrderDao {
    @Override
    public CutPageBean<OrderBean> cutAll(int pageNO) {
        CutPageBean<OrderBean> cutBean = new CutPageBean<>();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_order limit ?,?");
            ps.setObject(1, (pageNO - 1) * CutPageBean.PAGESIZE);
            ps.setObject(2, CutPageBean.PAGESIZE);

            rs = ps.executeQuery();
            while (rs.next()) {
                OrderBean orderBean = new OrderBean();
                orderBean.setId(rs.getInt("pk_orderId"));
                orderBean.setUserName(rs.getString("o_userName"));
                String time = rs.getString("o_orderDate");
                orderBean.setOrderDate(time != null ? LocalDate.parse(time) : null);
                orderBean.setAddress(rs.getString("o_address"));
                cutBean.getList().add(orderBean);

            }

            ps = con.prepareStatement("select  count(*) c from t_order ");
            rs = ps.executeQuery();
            if (rs.next()) {
                cutBean.setCount(rs.getInt("c"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }

        return cutBean;
    }

    @Override
    public void add(OrderBean order) {
        this.setConnection();
        try {
            this.ps = this.con.prepareStatement("insert into t_order(o_userName,o_orderDate,o_address) values (?,?,?)");
            this.ps.setObject(1, order.getUserName());
            this.ps.setObject(2, order.getOrderDate());
            this.ps.setObject(3, order.getAddress());
            this.ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
    }

    @Override
    public void del(int id) {
        this.setConnection();
        try {
            this.ps = this.con.prepareStatement("DELETE from t_order where pk_orderId=?");
            ps.setObject(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
    }

    @Override
    public void update(int id, String address) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update t_order set o_address=? where pk_orderId=?");

            ps.setObject(1, address);
            ps.setObject(2, id);

            ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
    }

    @Override
    public OrderBean findById(int id) {
        this.setConnection();
        try {
            ps = con.prepareStatement("select *from t_order where pk_orderId=?");
            ps.setObject(1,id);
            rs=ps.executeQuery();
            if (rs.next()){
                OrderBean order=new OrderBean();
                order.setId(rs.getInt("pk_orderId"));
                order.setUserName(rs.getString("o_userName"));
                String time = rs.getString("o_orderDate");
                order.setOrderDate(time != null ? LocalDate.parse(time) : null);
                order.setAddress(rs.getString("o_address"));
                return order;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
        return null;
    }
}
